/**
 * Copyright (C) 2022
 * All rights reserved, Designed By www.joolun.com
 * 注意：
 * 本软件为www.joolun.com开发研制，项目使用请保留此说明
 */
package com.joolun.web.controller.tennis;

import cn.hutool.core.convert.Convert;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.joolun.common.core.controller.BaseController;
import com.joolun.common.core.domain.AjaxResult;
import com.joolun.common.core.domain.entity.SysUser;
import com.joolun.common.core.domain.model.LoginUser;
import com.joolun.common.utils.ServletUtils;
import com.joolun.framework.web.service.TokenService;
import com.joolun.tennis.domain.TennisClass;
import com.joolun.tennis.service.TennisClassService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * spu商品
 *
 * @author JL
 * @date 2019-08-12 16:25:10
 */
@Slf4j
@RestController
@AllArgsConstructor
@RequestMapping("/tennisclass")
@Api(value = "tennisclass", tags = "课程管理")
public class TennisClassController extends BaseController {

    private final TennisClassService tennisClassService;

	@Autowired
	private TokenService tokenService;
    /**
    * 分页查询
    * @param page 分页对象
    * @param tennisClass spu商品
    * @return
    */
	@ApiOperation(value = "分页查询")
    @GetMapping("/page")
    @PreAuthorize("@ss.hasPermi('tennis:tennisclass:index')")
    public AjaxResult getGoodsSpuPage(Page page, TennisClass tennisClass) {
		LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
		SysUser user = loginUser.getUser();
		if(!user.isAdmin()&&!user.isCmsAdmin()){
			tennisClass.setUserId(user.getUserId());
		}
		return AjaxResult.success(tennisClassService.page1(page, tennisClass));
    }

	/**
	 * list查询
	 * @param tennisClass
	 * @return
	 */
	@ApiOperation(value = "list查询")
	@GetMapping("/list")
	public List<TennisClass> getList(TennisClass tennisClass) {
		LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
		SysUser user = loginUser.getUser();
		if(!user.isAdmin()&&!user.isCmsAdmin()){
		}
		return tennisClassService.list(Wrappers.query(tennisClass).lambda()
						.select(TennisClass::getId,
								TennisClass::getName)
				);
	}

	/**
	 * 查询数量
	 * @param tennisClass
	 * @return
	 */
	@ApiOperation(value = "查询数量")
	@GetMapping("/count")
	public AjaxResult getCount(TennisClass tennisClass) {
		return AjaxResult.success(tennisClassService.count(Wrappers.query(tennisClass)));
	}

    /**
    * 通过id查询spu商品
    * @param id
    * @return AjaxResult
    */
	@ApiOperation(value = "通过id查询课程")
    @GetMapping("/{id}")
    @PreAuthorize("@ss.hasPermi('tennis:tennisclass:get')")
    public AjaxResult getById(@PathVariable("id") String id){
        return AjaxResult.success(tennisClassService.getById1(id));
    }

    /**
    * 新增课程
    * @param tennisClass 课程对象
    * @return AjaxResult
    */
	@ApiOperation(value = "新增课程")
    @PostMapping
    @PreAuthorize("@ss.hasPermi('tennis:tennisclass:add')")
    public AjaxResult save(@RequestBody TennisClass tennisClass){
		LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
		SysUser user = loginUser.getUser();
		if(!user.isClubAdmin()&&!user.isAdmin()&&!user.isCmsAdmin()){
			return AjaxResult.error("权限不够，请联系管理员");
		}
        return AjaxResult.success(tennisClassService.save1(tennisClass));
    }

    /**
    * 修改课程
    * @param tennisClass 课程对象
    * @return AjaxResult
    */
	@ApiOperation(value = "修改课程")
    @PutMapping
    @PreAuthorize("@ss.hasPermi('tennis:tennisclass:edit')")
    public AjaxResult updateById(@RequestBody TennisClass tennisClass){
		LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
		SysUser user = loginUser.getUser();
		if(!user.isClubAdmin()&&!user.isAdmin()&&!user.isCmsAdmin()){
			return AjaxResult.error("权限不够，请联系管理员");
		}
        return AjaxResult.success(tennisClassService.updateById1(tennisClass));
    }

	/**
	 * 课程上下架操作
	 * @param shelf
	 * @param ids
	 * @return AjaxResult
	 */
	@ApiOperation(value = "商品上下架操作")
	@PutMapping("/shelf")
	@PreAuthorize("@ss.hasPermi('tennis:tennisclass:edit')")
	public AjaxResult updateById(@RequestParam(value = "shelf") String shelf, @RequestParam(value = "ids") String ids){
		TennisClass tennisClass = new TennisClass();
		tennisClass.setShelf(shelf);
		return AjaxResult.success(tennisClassService.update(tennisClass,Wrappers.<TennisClass>lambdaQuery()
				.in(TennisClass::getId, Convert.toList(ids))));
	}

    /**
    * 通过id删除课程
    * @param id
    * @return AjaxResult
    */
	@ApiOperation(value = "通过id删除课程")
    @DeleteMapping("/{id}")
    @PreAuthorize("@ss.hasPermi('tennis:tennisclass:del')")
    public AjaxResult removeById(@PathVariable String id){
        return AjaxResult.success(tennisClassService.removeById(id));
    }

}
